﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpellCaster3.Core;
using System.Reflection;
using Ultima;
using System.IO;

namespace SpellCaster3
{
    public partial class OptionsForm : Form
    {
        private StringCollection _UseCommandMethodsCollection = new StringCollection();

        public OptionsForm()
        {
            InitializeComponent();

            Type enumType = typeof(UseCommandMethods);
            foreach (string enumName in Enum.GetNames(enumType))
            {
                MemberInfo[] members = enumType.GetMember(enumName);
                string desc = string.Empty;
                if (members[0].GetCustomAttributes(typeof(DescriptionAttribute), false).Length > 0)
                {
                    object[] attrs = members[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                    desc = (attrs[0] as DescriptionAttribute).Description;
                }
                else
                    desc = members[0].Name;
                _UseCommandMethodsCollection.Add(desc);
            }

            commandMethodComboBox.DataSource = _UseCommandMethodsCollection;

            string enumValueName = Enum.GetName(typeof(UseCommandMethods), SpellCaster3.Properties.Settings.Default.UseCommandMethod);
            int i = 0;
            foreach (string s in Enum.GetNames(enumType))
            {
                if (s == enumValueName)
                    break;
                ++i;
            }
            commandMethodComboBox.SelectedIndex = i;

            customUoDirectoryTextBox.Text = SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory;
            if (!string.IsNullOrWhiteSpace(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory))
                customUoDirectoryFolderBrowserDialog.SelectedPath = SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory;

            if (SpellCaster3.Properties.Settings.Default.DescriptionInitialShowDelay < (int)descriptionShowDelayNumericBox.Minimum)
            {
                descriptionShowDelayNumericBox.Value = 1;
                SpellCaster3.Properties.Settings.Default.Save();
            }
            else if (SpellCaster3.Properties.Settings.Default.DescriptionInitialShowDelay > (int)descriptionShowDelayNumericBox.Maximum)
            {
                descriptionShowDelayNumericBox.Value = descriptionShowDelayNumericBox.Maximum;
                SpellCaster3.Properties.Settings.Default.Save();
            }
            else
                descriptionShowDelayNumericBox.Value = SpellCaster3.Properties.Settings.Default.DescriptionInitialShowDelay;

            hideIconsCheckBox.Checked = SpellCaster3.Properties.Settings.Default.HideIconsWhenFocusLost;
        }

        private void commandMethodComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            applyButton.Enabled = true;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            SaveConfig();
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            SaveConfig();
            applyButton.Enabled = false;
        }

        private void SaveConfig()
        {
            SpellCaster3.Properties.Settings.Default.UseCommandMethod = (UseCommandMethods)Enum.GetValues(typeof(UseCommandMethods)).GetValue(commandMethodComboBox.SelectedIndex);
            SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory = customUoDirectoryTextBox.Text;
            SpellCaster3.Properties.Settings.Default.DescriptionInitialShowDelay = (int)descriptionShowDelayNumericBox.Value;
            SpellCaster3.Properties.Settings.Default.HideIconsWhenFocusLost = hideIconsCheckBox.Checked;
            SpellCaster3.Properties.Settings.Default.Save();
            //SpellCaster3.Forms.IconForm._Descriptor.InitialDelay = SpellCaster3.Properties.Settings.Default.DescriptionInitialShowDelay;
            try
            {
                if (!string.IsNullOrWhiteSpace(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory))
                    Client.SetUltimaOnlineDirectory(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Attenzione, il percorso specificato non è una directory valida di Ultima Online, valore resettato", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                customUoDirectoryTextBox.Text = "";
                SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory = customUoDirectoryTextBox.Text;
                SpellCaster3.Properties.Settings.Default.Save();
            }
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
            applyButton.Enabled = false;
        }

        private void customUoDirectoryButton_Click(object sender, EventArgs e)
        {
            if (customUoDirectoryFolderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                customUoDirectoryTextBox.Text = customUoDirectoryFolderBrowserDialog.SelectedPath;
                customUoDirectoryTextBox_Validating(sender, new CancelEventArgs(false));
            }
        }

        private void customUoDirectoryTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(customUoDirectoryTextBox.Text)) return;

            string path = customUoDirectoryTextBox.Text;

            e.Cancel = !Client.IsValidUltimaOnlineDirectory(path);
            if (e.Cancel)
            {
                customUoDirectoryTextBox.Text = "";
                ErrorForm.Show("Percorso non valido, la directory deve contenere un file client.exe o Client.exe e deve esistere");
            }
        }

        private void customUoDirectoryTextBox_Validated(object sender, EventArgs e)
        {
            customUoDirectoryFolderBrowserDialog.SelectedPath = customUoDirectoryTextBox.Text;
        }

        private void descriptionShowDelayNumericBox_ValueChanged(object sender, EventArgs e)
        {
            applyButton.Enabled = true;
        }

        private void customUoDirectoryTextBox_TextChanged(object sender, EventArgs e)
        {
            applyButton.Enabled = true;
        }

        private void hideIconsCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            applyButton.Enabled = true;
        }
    }
}
